/**
 * 合并两个有序数组
 */

const arr1 = [1,2,3]
const arr2= [4,5,6]


function climbArr(arr1,arr2) {
    const length1=arr1.length, length2 = arr2.length

    // 如果
    if(arr1[0]>arr2[length2-1]){
        return [...arr2,...arr1]
    }
    if(arr1[length1-1]<arr2[0]){
        return [...arr1,...arr2]
    }
    let i=0,j=0,res=[]
    while(i<length1&&j<length2){
        if(arr1[i]<arr2[j]){
            res.push(arr1[i])
            i++
        }else {
            res.push(arr2[j])
            j++
        }
    }
    if(length1>length2){
        res.push(arr2.slice(length2,length1-length2))
    }else{
        res.push(arr1.slice(length1,length2-length1))
    }

    return res;
}

console.log(climbArr(arr1,arr2))


function climbArr2(arr1,arr2) {
    if(!arr1.length){
        return arr2;
    }

    if(!arr2.length){
        return arr1;
    }

    let cur=null,i=arr1.length-1, j= arr2.length-1,tail=i+j-1,res=[]

    while(i>=0 || j>=0){
        if(i===-1){
            cur = arr2[j--]
        }else if(j===-1){
            cur = arr1[i--]
        }else if(arr1[i]>arr2[j]){
            cur = arr1[i--]
        }else {
            cur = arr2[j--]
        }
        res[tail--] = cur;
    }
    return res;
}